import { quest } from "../../utils/http";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: [
      { id: 1, name: "小件", select: true, money: 3 },
      { id: 2, name: "中件", select: false, money: 5 },
      { id: 3, name: "大件", select: false, money: 7 },
    ],
    delivery: [],
    currentDelivery: [],
    detail: {},
    gender: [
      { id: 0, gender: "女", select: false },
      { id: 1, gender: "男", select: false },
      { id: 2, gender: "不限", select: false },
    ],
    myGender: { id: 2, gender: "不限", select: false },
    money: "3",
    page: "",
    rushId: Number,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let user = await quest({
      url: "token/getClaim",
      method: "post",
    });
    let university = await quest({
      url: "university/list",
    });
    if (university.code == 200) {
      university.data.items.forEach((i) => {
        if (user.universityId == i.id) {
          i.items.forEach((k) => {
            if (k.deliveryAddr == "跑腿") {
              console.log(k.id);
              this.setData({
                rushId: k.id,
              });
            }
          });
        }
      });
    }

    if (options.name == "rush") {
      this.setData({
        type: [],
        page: "跑腿",
      });
      return;
    }
    let delivery = await quest({
      url: "delivery/list/byUser",
    });
    if (delivery.code == 200) {
      this.setData({
        delivery: delivery.data.items,
        page: "快递代拿",
      });
    }
  },
  remarkChange(e) {
    // console.log(e.detail.value);
    let detail = this.data.detail;
    detail.remark = e.detail.value;
    this.setData({ detail });
  },
  bindPickerChange(e) {
    // console.log(e.detail.value);
    let delivery = this.data.delivery[e.detail.value];
    this.setData({
      currentDelivery: delivery,
    });
  },
  recAddrChange(e) {
    let detail = this.data.detail;
    detail.recAddr = e.detail.value;
    this.setData({ detail });
  },
  bindGenderChange(e) {
    // console.log(e.detail.value);
    let myGender = this.data.gender[e.detail.value];
    this.setData({
      myGender,
    });
  },
  selectType(e) {
    let { id } = e.currentTarget.dataset;
    let { type, money } = this.data;
    type.forEach((element) => {
      if (element.id == id) {
        element.select = true;
        money = element.money;
      } else {
        element.select = false;
      }
    });
    this.setData({
      type,
      money,
    });
  },
  async modifyUpdate() {
    let { rushId, detail, type, currentDelivery, myGender, money } = this.data;
    detail.deliveryId = currentDelivery.id;
    detail.requireGender = myGender.id;
    detail.status = 0;
    if (this.data.page != "跑腿") {
      detail.price = money;
    }
    if (this.data.page == "跑腿") {
      detail.deliveryId = rushId;
    }
    detail.title = this.data.page;
    type.forEach((element) => {
      if (element.select == true) {
        detail.type = element.id;
      }
    });
    let data = await quest({
      url: "express/put",
      method: "put",
      data: {
        ...detail,
      },
    });

    console.log(data);
    if (data.code == 200) {
      wx.navigateBack({
        delta: 1,
      });
    }
  },
  priceChange(e) {
    let detail = this.data.detail;
    detail.price = e.detail.value;
    this.setData({ detail });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
